package cn.edu.sjtu.se.dslab.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.dao.IBusZoneDao;
import cn.edu.sjtu.se.dslab.dao.IEntityZoneDao;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.project.BusZone;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IBusZoneService;

@Service("busZoneService")
@Transactional
public class BusZoneServiceImpl implements IBusZoneService {

	@Resource(name = "busZoneDao")
	private IBusZoneDao busZoneDao;
	
	@Resource(name = "entityZoneDao")
	private IEntityZoneDao entityZoneDao;
	
	@Resource(name="projectDao")
	private IProjectDao projectDao;
	
	

	@Override
	public boolean addBusZone(BusZone busZone, long projectId) {
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project id not found " + projectId);
		busZone.setProject(project);
		busZoneDao.save(busZone);
		return true;
	}

	@Override
	public BusZone updateBusZone(BusZone busZone) {
		return busZoneDao.update(busZone);
	}

	@Override
	public BusZone getBusZoneById(long id) {
		return busZoneDao.queryById(id);
	}

	@Override
	public BusZone getBusZoneByName(String name) {
		return busZoneDao.queryByName(name);
	}

	@Override
	public List<BusZone> getBusZoneList() {
		return busZoneDao.queryByList();
	}

	@Override
	public boolean deleteBusZoneById(long busZoneId) {
		BusZone busZone = busZoneDao.queryById(busZoneId);
		if (null != busZone) {
			busZoneDao.delete(busZone);
			return true;
		}
		return false;
	}

	@Override
	public List<BusZone> getBusZoneByProject(long projectId) {
		return busZoneDao.queryByProject(projectId);
	}

	@Override
	public Set<EntityZone> getEntityZoneSet(long busZoneId) {
		BusZone busZone = getBusZoneById(busZoneId);
		if (busZone == null) throw new NullPointerException("Bus Zone can't be found:" + busZoneId);
		String entityZoneIds = busZone.getEntityZones();
		if(entityZoneIds == null || entityZoneIds.equals("")) return null;
		String[] ids = entityZoneIds.split("\\|");
		Set<EntityZone> entityZoneSet = new HashSet<EntityZone>();
		for (String id : ids){
			if (id.equals("") || id.equals("|") ) continue;
			EntityZone entityZone = entityZoneDao.queryById(Long.parseLong(id));
			if (entityZone != null){
				entityZoneSet.add(entityZone);
			}
		}
		return entityZoneSet;
	}

	@Override
	public void addEntityZone(long busZoneId, List<EntityZone> entityZoneList) {
		BusZone busZone = getBusZoneById(busZoneId);
		if (busZone == null) throw new NullPointerException();
		String entityZoneIds = busZone.getEntityZones();
		Set<Long> entityZoneSet = new HashSet<Long>();
		if(entityZoneIds != null && !entityZoneIds.equals("")){
			String[] ids = entityZoneIds.split("|");
			for (String s: ids){
				entityZoneSet.add(Long.parseLong(s));
			}
		}
		for(EntityZone entityZone : entityZoneList){
			entityZoneSet.add(entityZone.getId());
		}
		StringBuilder sb = new StringBuilder();
		for(long id : entityZoneSet){
			sb.append(id).append("|");
		}
		busZone.setEntityZones(sb.toString());
		updateBusZone(busZone);
	}
	
	@Override
	public BusZone getBusZoneByProjectAndName(long projectId, String name){
		return busZoneDao.queryByProjectAndName(projectId, name);
	}

}

 

















